/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-08-22 16:18:21
 * @LastEditors: turingchang
 * @LastEditTime: 2020-09-04 20:55:04
 */

'use strict';


const BaseController = require('./base/base');

class TestController extends BaseController {

  async index() {
    //this.ctx.redirect('../../index.html');
  }

  async test() {
    var data = {
      name: 'uuuuuuu',
      pwd: 'adsfafafasd',
      gg: null,
      jj: undefined,
      h: {},
      ro: {
        xxx: 0,
        ccc: null
      }
    };
    // var data = this.app.utils.time.getTimeSpan();   
    //var data =this.app.utils.generator.avatars('书',null,null,null);
    //var data =this.app.utils.generator.generateUid(false);   
    //var data = this.app.utils.string.zhmoney.cn.toMoney("1");   
    //data = this.app.utils.string.pinyin.convert('QQQQ这是一个汉语拼音库',0, false, false, ' ');
    //data =this.app.utils.generator.svgCaptcha.create();    
    //data ==this.app.utils.emoji[0];
    //data = this.app.utils.string.encode(this.app.utils.emoji[0]);
    //var time ='2020-08-24 23:36:10';
    //data =this.app.utils.time.formatMoment(time);
    //var arr =[1,2,3,4,2,6,4];
    //data = this.app.utils.array.deduplication(arr);
    //this.app.getLogger('requestLogger').info(data);
    //this.app.getLogger('requestLogger').debug(data);
    //this.app.logger.info('666666');
    //this.ctx.requestLogger.error(data,'error info me ');
    //console.log(this.ctx.app.config.hasOwnProperty("jwt"));
    //console.log(this.ctx.app.config["jwt"]);
    console.log('彩虹屁的彩虹屁'.rainbow);
    // console.log(this.ctx.app.utils.number.formatCurrency(33.84978));
    // console.log(this.ctx.app.utils.number.format(33,2));
    // console.log(111,typeof data);
    // var value ='0014ewtsdg';
    // var pattern = /^[a-z0-9]{4,20}$/;
    // if (pattern.test(value)) {
    //   console.log('yes');
    // }else{
    //   console.log('no');
    // }

    this.ctx.outSuccess(null, obj);
  }

  rebuild(data, obj) {
    var hc =false
    Object.keys(data).forEach((element) => {
      hc =false
      if(!this.isNum(element) && element.length>1) {
        obj[element] ={}
        obj[element]['children'] =[] 
        obj[element]['sum_delta'] =null 
        obj[element]['sum_gamma'] =null 
        obj[element]['sum_vega'] =null 
        obj[element]['sum_theta'] =null 
        obj[element]['sum_unrealized_pnl'] =null
        Object.keys(data[element]).forEach((el,index,item) =>{
          // console.log(el,data[element][el])
          if(this.isNum(el) || el.length<2){
            hc =true
            obj[element]['children'].push(data[element][el])
            if(data[element][el]['delta']){
              obj[element]['sum_delta'] ==null ?0 : obj[element]['sum_delta']
              obj[element]['sum_gamma'] =null ? 0 : obj[element]['sum_gamma'] 
              obj[element]['sum_vega'] =null ?0 :obj[element]['sum_vega']
              obj[element]['sum_theta'] =null ?0 :obj[element]['sum_theta'] 

              obj[element]['sum_delta']+=data[element][el]['delta'];
              obj[element]['sum_gamma']+=data[element][el]['gamma'];
              obj[element]['sum_vega']+=data[element][el]['vega'];
              obj[element]['sum_theta']+=data[element][el]['theta'];
              obj[element]['sum_unrealized_pnl']+=data[element][el]['unrealized_pnl'];
            }
            //code + code =sum
          }else{
            return;
          }
        })
        if(!hc){
          this.rebuild(data[element],obj[element])
        }
        //剔除空对象
        if (obj[element].children.length <= 0) {
          delete obj[element].children
        }    
        if (obj[element]['sum_delta'] ==null) {
          delete obj[element]['sum_delta']
        }   
        if (obj[element]['sum_gamma'] ==null) {
          delete obj[element]['sum_gamma']
        }  
        if (obj[element]['sum_vega'] ==null) {
          delete obj[element]['sum_vega']
        }  
        if (obj[element]['sum_theta'] ==null) {
          delete obj[element]['sum_theta']
        }  
        if (obj[element]['sum_unrealized_pnl'] ==null) {
          delete obj[element]['sum_unrealized_pnl']
        }  
      }
    });
  }

  isNum(s){
    if (s) {
      return !isNaN(s);
    }
    return false;
  }



}

module.exports = TestController;
